package com.newland.zxy;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangxuyuan
 * @version 1.0
 * @date 2021/1/12
 */
public class UserDao {
    private static Logger logger = LogManager.getLogger(UserDao.class);

    public static List<User> getUserList() throws SQLException {
        Connection con = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            List<User> userList = new ArrayList<>();
            String sql = "SELECT * FROM UIG_TESTNG_USER t";
            con = DBUtil.getCon();
            preparedStatement = con.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                String userid = resultSet.getString("USERID");
                String username = resultSet.getString("USERNAME");
                String custid = resultSet.getString("CUSTID");
                String accountid = resultSet.getString("ACCOUNTID");
                logger.info(userid + "," + username + "," + custid + "," + accountid);
                User user = new User();
                user.setAccountId(accountid);
                user.setCustId(custid);
                user.setUserId(userid);
                user.setUserName(username);
                userList.add(user);
            }
            logger.info("userList size " + userList.size());
            return userList;
        } finally {
            DBUtil.close(con, preparedStatement, resultSet);
        }
    }

}
